package com.xzw.cameratest.async;

import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleObserver;
import android.arch.lifecycle.OnLifecycleEvent;
import android.os.AsyncTask;

/**
 * @description:
 * @author:111144
 * @date:2020/5/28
 */
public class AsyncLifecycleObserver implements LifecycleObserver {
    private AsyncBuilder asyncBuilder;
    public AsyncLifecycleObserver(AsyncBuilder asyncBuilder){
        this.asyncBuilder = asyncBuilder;
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    public void  onLifeCycleCreate() {
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void  onLifeCycleDestroy() {
        if (asyncBuilder != null && asyncBuilder.getStatus() != AsyncTask.Status.FINISHED){
            asyncBuilder.cancel(true);
        }
    }
}
